#!/usr/local/bin/python
#-*- encoding:utf-8 -*- 
import codecs 
from whoosh.index import create_in  
from whoosh.fields import *  
from chinesetokenizer import ChineseAnalyzer
import random
from whoosh.index import open_dir
from whoosh import qparser


def cmp(t1, t2):
    k1, v1  = t1;
    k2, v2 = t2;
    return len(v2) - len(v1);

def getScore(total, pos):
    bar = total/3;
    if pos < bar:
        return 3;
    elif pos < bar*2:
        return 2;
    else:
        return 1;
def computeDis(results, value):
    setA = set([hit.fields()['appid'] for hit in results]);
    setB = set(value.keys());
    l = len(setA.intersection(setB));

    return l/float(100);

queryDict = {}
with open('../operLog', 'r') as f:
    for line in f:
        li = line.strip().split('\t');
        if len(li) !=3:
            continue;
        appID,oper , query = line.strip().split('\t');
        if queryDict.has_key(query):
            if queryDict[query].has_key(appID):
                queryDict[query][appID] = queryDict[query][appID] + 1;
            else:
                queryDict[query][appID] = 1;
        else:
            queryDict[query] = {appID:1};
li = sorted(queryDict.items(), cmp);

#ix = open_dir("IndexDir/titleIndex");
#parser = qparser.QueryParser('content',ix.schema, group=qparser.syntax.OrGroup);
#searcher = ix.searcher();
with open('finalunmatchLogStatistical', 'w') as filter:
    for key ,value in li:
        #if len(value) > 5 and random.random() < 0.01:
        sortedValue = sorted(value.items(), key=lambda x: x[1], reverse=True);
        #query = parser.parse(key.decode('utf-8'));
        #results = searcher.search(query, limit=100);
        #dis =  computeDis(results, value)    
        #l = len(sortedValue);
         
       #sortedValue = map(lambda x:(x[1][0], getScore(l, x[0])), enumerate(sortedValue));
        dis = 0;
        filter.write('%f\t%s|%s\n' %(dis,key, '|'.join(['%s:%s:%d'%(p, pair[0], pair[1]) for p, pair in enumerate(sortedValue, start=1)])));

